import { useState, useEffect } from "react";
import CommonModal from "@/components/CommonModal";
import SubTitle from "@/pages/OrderModule/common/components/SubTitle";
import { Select, Form, message, Modal } from "antd";
import { getExpressRelativePrinter, setExpressRelativePrinter } from "@/utils/authority";
import { getAllPrinter } from "@/utils/print.jsx";
import { saveCodePrintTask } from '../service';
import { printKmUniqcode } from '@/utils/printKm.js';
import './index.less';

const PrintKmModal = ({ printConfig, updatePrintStatus, hanldeCancel }) => {

	const { printData, savePrintData, printNum } = printConfig;

	//默认打印机列表
	const printObj = getExpressRelativePrinter() || {};

	const [form] = Form.useForm();

	const [open, setOpen] = useState(false);

	//打印机
	const [printList, setPrintList] = useState([]);

	const [isPrint, setIsPrint] = useState(false);

	//保存打印记录
	const savePrintRecord = async (printData, printer) => {
		await saveCodePrintTask({
			printerName: printer,
			printPageType: 4,
			codePrintSaveVOList: printData
		})
		const arr = savePrintData.map(v => v.uniqueCode);
		updatePrintStatus && updatePrintStatus(arr);
	}

	useEffect(() => {
		setOpen(true);
		const currentPrinter = printObj['唯一码'];
		getAllPrinter(val => {
			setPrintList(val);
			if (!currentPrinter) {
				return;
			}
			if (val.includes(currentPrinter)) {
				form.setFieldsValue({
					'printer': currentPrinter
				})
			} else {
				message.warning(`唯一码关联的打印机${currentPrinter}未开启`);
			}
		});
	}, [])


	// 设置打印机名称
	const handlePrinterName = name => {
		setExpressRelativePrinter({
			...printObj,
			'唯一码': name
		})
	};

	//获取打印数据推入队列
	const onOk = () => {
		if(isPrint) {
			return Modal.info({
				content: `${printNum}个条码正在打印，请勿重复操作`,
			})
		}
		if (!printData?.length) {
			return message.info("打印数据为空");
		}
		form.validateFields().then(value => {
			if (!value) {
				return message.info("请选择打印机");
			}
			message.success(`即将打印${printNum}个条码，请等待`);
			setIsPrint(true);
			printKmUniqcode(printData, value.printer, () => {
				setOpen(false);
				savePrintRecord(savePrintData, value.printer);
			});
		});
	};

	const onCancel = () => {
		setOpen(false);
	}

	const afterClose = () => {
		hanldeCancel();
	}

	const options =
		Array.isArray(printList) &&
		printList.map(d => (
			<Option key={d} value={d}>
				{d}
			</Option>
		));

	return (
		<CommonModal
			title="唯一码打印"
			open={open}
			width="552px"
			onOk={onOk}
			onCancel={onCancel}
			afterClose={afterClose}
		>
			<Form form={form} name="common-printer-select">
				<SubTitle symbol as="s2" title={`打印详情`} wrapStyle={{ marginBottom: 16 }} />
				<Form.Item style={{ margin: 8 }}>
					<div>
						<span>打印唯一码数量：</span>
						<span style={{ color: "rgba(240, 61, 41, 1)" }}>{printNum}</span>
					</div>
				</Form.Item>
				<Form.Item label="" name="printer" style={{ margin: 8 }} rules={[{ required: true, message: "请先选择打印机" }]}>
					<Select placeholder="请选择打印机" onChange={handlePrinterName} >
						{options}
					</Select>
				</Form.Item>
			</Form>
		</CommonModal>
	);
};

export default PrintKmModal;
